package com.ycy.dynamic;

import com.ycy.BaseTest;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.impl.dynamic.DynamicUserTaskBuilder;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Test;

import java.util.*;

/**
 * 动态添加节点
 */
public class CustomInjectUserTaskInProcessInstanceCmdDemo extends BaseTest {

  @Test
  public void createDef() {
    // 创建Flowable流程引擎 Create Flowable process engine
    ProcessEngine processEngine = cfg.buildProcessEngine();
    // 获取Flowable服务 Get Flowable repositoryService
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deploy = repositoryService.createDeployment().addClasspathResource("dynamic/easy_key.bpmn20.xml").deploy();
    System.out.println(deploy.getId());
  }

  @Test
  public void creatProcInst() {
    RuntimeService runtimeService = processEngine.getRuntimeService();
    HashMap<String, Object> variables = new HashMap<>();
    List<String> zhixingrens = new ArrayList<>();
    zhixingrens.add("ycy");
    zhixingrens.add("xdm");
    variables.put("ASSIGNEE_zx_LIST", zhixingrens);

    ProcessInstance processInstance =
            runtimeService.startProcessInstanceByKey("easy_key_1", variables);
    System.out.println(processInstance.getProcessInstanceId());
  }

  /**
   * 在通过审批通过节点后面添加节点
   */
  @Test
  public void addAfterpass() {

    //新增一个节点
    String dynamicUserId = "UserTaskAdd-" + UUID.randomUUID().toString().replaceAll("-", "");
    DynamicUserTaskBuilder dynamicUserTaskBuilder = new DynamicUserTaskBuilder();
    dynamicUserTaskBuilder.setId(dynamicUserId);
    dynamicUserTaskBuilder.setName("新增节点");
    dynamicUserTaskBuilder.setAssignee("632511");

//  processEngine.getDynamicBpmnService().injectUserTaskInProcessInstance();
    String processInstanceId = "27501";
    List<Task> list = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).list();

    String processDefinitionId = list.get(0).getProcessDefinitionId();
    System.out.println("流程定义 " + processDefinitionId);
    BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(processDefinitionId);
    Process process = bpmnModel.getMainProcess();

    FlowElement flowElement = process.getFlowElementMap().get("zx");
    Void unused = processEngine.getManagementService().executeCommand(
            new CustomInjectUserTaskInProcessInstanceCmd(processInstanceId, dynamicUserTaskBuilder, flowElement));
  }

  @Test
  public void afterSignUserTaskCmd() {
    DynamicUserTaskBuilder dynamicUserTaskBuilder = new DynamicUserTaskBuilder();
    List<Task> list = processEngine.getTaskService().createTaskQuery().processDefinitionKey("easy_key_1").list();
    for (Task task : list) {
      System.out.println(task);
    }
    AfterSignUserTaskCmd afterSignUserTaskCmd = new AfterSignUserTaskCmd(dynamicUserTaskBuilder,"10004", "2501");
    Void unused = processEngine.getManagementService().executeCommand(afterSignUserTaskCmd);
  }



  //审批 通过```
  @Test
  public void pass() {
    TaskService taskService = processEngine.getTaskService();
    List<Task> tasks = taskService.createTaskQuery().processInstanceId("2501").list();
    System.out.println("You have " + tasks.size() + " tasks:");
    for (int i = 0; i < tasks.size(); i++) {
      Task task = tasks.get(i);
      System.out.println((i + 1) + ") " + task.getId() + " : " + task.getName());
      System.out.println(task);

      //如果参数设置错误会怎么样？
      Map<String, Object> variables = new HashMap<String, Object>();
      variables.put("approved", true);
      taskService.complete(task.getId(), variables);
    }
  }


}
